package com.solution.admin.sysconfig.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.sysconfig.entity.WechatMsgEventReplyEntity;
import com.solution.admin.sysconfig.service.WechatMsgEventReplyService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;
import com.solution.wx.core.constants.WechatConstants.RECEIVE_EVENT_TYPE;

/**
 * 微信接收事件推送web控制层
 * @author solin
 * @date 2017-11-30 14:41:21
 */
@RequestMapping("/wechatMsgEventReply")
@Controller("wechatMsgEventReplyController")
public class WechatMsgEventReplyController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(WechatMsgEventReplyController.class);

	@Autowired
	private WechatMsgEventReplyService wechatMsgEventReplyService;
	
	/**
	 * 跳转微信接收事件推送查询页面
	 * @param WechatMsgEventReply 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-11-30 14:41:21
	 */
	@RequestMapping("/toSubscribeReplyPage")
	public String toSubscribeReplyPage(WechatMsgEventReplyEntity wechatMsgEventReply, HttpServletRequest request) {
		return "sysconfig/wechatMsgEventReply_list";
	}

	/**
	 * 分页查询微信接收事件推送列表
	 * @param wechatMsgEventReply 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-11-30 14:41:21
	 */
	@RequestMapping("/listSubscribeReplyPage")
	@ResponseBody
	public JSONGrid listSubscribeReplyPage(WechatMsgEventReplyEntity wechatMsgEventReply, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			paramMap.put("eventType", RECEIVE_EVENT_TYPE.SUBSCRIBE);
			PageBean pageBean = wechatMsgEventReplyService.listWechatMsgEventReplyPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询微信接收事件推送失败", logger, e);
		}
		return new JSONGrid();
	}
	
	/**
	 * 新增或保存微信接收事件推送
	 * @param request 请求对象
	 * @param wechatMsgEventReplyEntity 微信接收事件推送实体
	 * @return
	 * @author solin
 	 * @date 2017-11-30 14:41:21
	 */
	@RequestMapping("/saveOrUpdateWechatMsgEventReply")
	@ResponseBody
	public Map<String, Object> saveWechatMsgEventReply(HttpServletRequest request, WechatMsgEventReplyEntity wechatMsgEventReplyEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				wechatMsgEventReplyEntity.setModifier(this.getUserCode());
				result = wechatMsgEventReplyService.updateWechatMsgEventReply(wechatMsgEventReplyEntity);
			}else{
				wechatMsgEventReplyEntity.setCreater(this.getUserCode());
				wechatMsgEventReplyEntity.setStatus(STATUS.ENABLED.getId());
				wechatMsgEventReplyEntity.setEventType(RECEIVE_EVENT_TYPE.SUBSCRIBE);
				result = (int)wechatMsgEventReplyService.saveWechatMsgEventReply(wechatMsgEventReplyEntity);
			}
			resultMap.put(PublicStatus.RESULT_CODE, result > 0 ? 1 : 0);
			resultMap.put(PublicStatus.RESULT_MSG, "保存成功");
		} catch (Exception e) {
			resultMap.put(PublicStatus.RESULT_CODE, 0);
			resultMap.put(PublicStatus.RESULT_MSG, "保存失败");
			handleException("添加微信接收事件推送失败", logger, e);
		}
		return resultMap;
	}
	
}
